package com.chpro.common;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Locale;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;

public class FreeMarkerUtils {

	private static Configuration queryConfig = new Configuration();

	static {
		queryConfig.setLocale(Locale.SIMPLIFIED_CHINESE);
		queryConfig.setDefaultEncoding("UTF-8");
		queryConfig.setClassForTemplateLoading(FreeMarkerUtils.class, "/template");
	}

	public static String processQryTpl(Map<String, String> map, String tplName) {
		Template tpl = null;
		try {
			tpl = queryConfig.getTemplate(tplName);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}

		StringWriter sw = new StringWriter();
		try {
			tpl.process(map, sw);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return sw.toString();
	}
}
